package com.ip.user.api;

import com.ip.user.models.dto.ChangePasswordDTO;
import com.ip.user.models.dto.UpdateProfileDTO;
import com.ip.user.models.service.SysUserService;
import com.ip.user.models.vo.UserVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人中心控制器
 * author 刘丰源
 */
@RestController
@RequestMapping("/api/profile")
public class ProfileController {
    
    @DubboReference
    private SysUserService sysUserService;
    
    /**
     * 修改密码
     * @param changePasswordDTO 修改密码DTO对象
     * @return 结果Map
     */
    @PostMapping("/change-password")
    public Map<String, Object> changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) {
        Map<String, Object> result = new HashMap<>();
        
        boolean success = sysUserService.changePassword(changePasswordDTO);
        
        if (success) {
            result.put("code", 200);
            result.put("message", "密码修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "密码修改失败，请检查旧密码是否正确");
        }
        
        return result;
    }
    
    /**
     * 更新个人信息（邮箱和电话）
     * @param updateProfileDTO 更新个人信息DTO对象
     * @param principal 当前认证用户信息
     * @return 结果ResponseEntity
     */
    @PostMapping("/update-profile")
    public ResponseEntity<Map<String, Object>> updateProfile(
            @RequestBody UpdateProfileDTO updateProfileDTO, 
            Principal principal) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 验证当前用户只能更新自己的信息
            String currentUserName = principal.getName();
            UserVO currentUser = sysUserService.getUserByUserName(currentUserName);
            
            if (!currentUser.getUserId().equals(updateProfileDTO.getUserId())) {
                result.put("success", false);
                result.put("message", "无权限更新其他用户的信息");
                return ResponseEntity.status(403).body(result);
            }
            
            // 创建UserBO对象用于更新
            com.ip.user.models.bo.UserBO userBO = new com.ip.user.models.bo.UserBO();
            userBO.setUserId(updateProfileDTO.getUserId());
            userBO.setPhone(updateProfileDTO.getPhone());
            userBO.setEmail(updateProfileDTO.getEmail());
            
            // 调用服务更新用户信息
            UserVO updatedUser = sysUserService.updateUser(userBO);
            
            if (updatedUser != null) {
                result.put("success", true);
                result.put("message", "个人信息更新成功");
                result.put("data", updatedUser);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "个人信息更新失败");
                return ResponseEntity.status(500).body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "个人信息更新失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }
}